package com.cool.reggie.modules.setmeal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cool.reggie.core.exception.CustomException;
import com.cool.reggie.modules.setmeal.dto.SetmealDto;
import com.cool.reggie.modules.setmeal.entity.SetmealEntity;
import com.cool.reggie.modules.setmeal.mapper.SetmealMapper;
import com.cool.reggie.modules.setmeal.service.SetmealService;
import com.cool.reggie.modules.setmealdish.entity.SetmealDishEntity;
import com.cool.reggie.modules.setmealdish.mapper.SetmealDishMapper;
import com.cool.reggie.modules.setmealdish.service.SetmealDishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 1950735817@qq.com
 * @since 2022-07-06
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, SetmealEntity> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void saveWithSetmealDto(SetmealDto setmealDto) {
        // 插入一条数据
        setmealMapper.myInsertOne(setmealDto);

        List<SetmealDishEntity> list = setmealDto.getSetmealDishes();
        list = list.stream().peek((item) -> {
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            item.setSetmealId(setmealDto.getId());
        }).collect(Collectors.toList());

        // 插入多条数据
        // 自己写SQL，不会经过Mybatis的Aop切面，因此无法自动生成时间以及id
        setmealDishMapper.myInsertMult(list);
    }


    /**
     * 自定义 xml 分页查询
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param name     名字
     * @return {@link Page}<{@link SetmealDto}>
     */
    @Override
    public Page<SetmealDto> myPageQuery(int page, int pageSize, String name) {
        List<SetmealDto> listPage = setmealMapper.getListPage((page - 1) * pageSize, pageSize, name);
        Long totalCount = setmealMapper.getTotalCount();

        Page<SetmealDto> pageWrapper = new Page<>();
        pageWrapper.setCurrent(page);
        pageWrapper.setSize(pageSize);
        pageWrapper.setRecords(listPage);
        pageWrapper.setTotal(totalCount);

//        LambdaQueryWrapper<SetmealEntity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotBlank(name),SetmealEntity::getName,name);
//        Page<SetmealEntity> setmealEntityPage = setmealMapper.selectPage(new Page<SetmealEntity>(page, pageSize), queryWrapper);

        return pageWrapper;
    }

    @Override
    public SetmealDto getByIdWithDishes(Long id) {
        SetmealEntity setmealEntity = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmealEntity, setmealDto);

        LambdaQueryWrapper<SetmealDishEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDishEntity::getSetmealId, setmealEntity.getId());

        List<SetmealDishEntity> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public boolean saveByIdWithDishes(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDishEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDishEntity::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        List<SetmealDishEntity> list = setmealDto.getSetmealDishes();

        list = list.stream().peek((item) -> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());

        return setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    public void deleteByIdWithSetmeal(List<Long> ids) {
        LambdaQueryWrapper<SetmealEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealEntity::getId, ids);
        queryWrapper.eq(SetmealEntity::getStatus, 1);

        long count = this.count(queryWrapper);

        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        this.setmealMapper.deleteBatchIds(ids);

        LambdaQueryWrapper<SetmealDishEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDishEntity::getSetmealId, ids);
        this.setmealDishMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public void stopByIdWithSetmeal(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new CustomException("至少选择一个套餐");
        }
//        LambdaQueryWrapper<SetmealEntity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(SetmealEntity::getId, ids);
//
//        this.update(queryWrapper);

        int status = 0;
        this.setmealMapper.updateByBatch(ids, status);
    }
}
